<?php

namespace app\common\model\card;

use think\Model;


class Entitylog extends Model
{

    

    

    // 表名
    protected $name = 'card_entitylog';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'transfer_time_text',
        'transfer_data_text',
        'status_text',
        'exchange_time_text',
        'exchange_start_time_text',
        'exchange_end_time_text'
    ];
    

    
    public function getTransferDataList()
    {
        return ['0' => __('Transfer_data 0'), '1' => __('Transfer_data 1'), '2' => __('Transfer_data 21'), '3' => __('Transfer_data 31')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getTransferTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['transfer_time']) ? $data['transfer_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTransferDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['transfer_data']) ? $data['transfer_data'] : '');
        $list = $this->getTransferDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExchangeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['exchange_time']) ? $data['exchange_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExchangeStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['exchange_start_time']) ? $data['exchange_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExchangeEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['exchange_end_time']) ? $data['exchange_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setTransferTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setExchangeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setExchangeStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setExchangeEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function site()
    {
        return $this->belongsTo('app\common\model\card\entity\Site', 'card_entity_site_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function member()
    {
        return $this->belongsTo('app\common\model\Member', 'member_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function dealer()
    {
        return $this->belongsTo('app\common\model\card\Dealer', 'card_dealer_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    protected static function init()
    {
        $log=Entitylog::where("exchange_end_time< ".time())->whereIn('status',['1','2'])->where("card_dealer_id >0 ")->find();
        if(!empty($log)){
            Entitylog::where("exchange_end_time< ".time())->whereIn('status',['1','2'])->where("card_dealer_id >0 ")->update(["status"=>'2']);
        }
    }
}
